package cn.ch.train.member.service;

import cn.ch.train.common.context.LoginMemberContext;
import cn.ch.train.common.response.PageResult;
import cn.ch.train.common.util.SnowUtil;
import cn.ch.train.member.domain.Passenger;
import cn.ch.train.member.domain.PassengerExample;
import cn.ch.train.member.mapper.PassengerMapper;
import cn.ch.train.member.request.PassengerQueryParam;
import cn.ch.train.member.request.PassengerSaveParam;
import cn.ch.train.member.response.PassengerQueryResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class PassengerService {
    private static final Logger logger = LoggerFactory.getLogger(PassengerService.class);
    @Resource
    private PassengerMapper passengerMapper;

    /**
     * description：保存乘客信息
     * @param param 新增/编辑乘客信息的入参
     */
    public void save(PassengerSaveParam param) {
        DateTime now = DateTime.now();
        Passenger passenger = BeanUtil.copyProperties(param, Passenger.class);
        if (Objects.isNull(passenger.getId())) {
            passenger.setMemberId(LoginMemberContext.getId());
            passenger.setId(SnowUtil.getSnowFlakeNextId());
            passenger.setCreateTime(now);
            passenger.setUpdateTime(now);
            passengerMapper.insert(passenger);
        } else {
            passenger.setUpdateTime(now);
            passengerMapper.updateByPrimaryKey(passenger);
        }
    }

    /**
     * description：查询乘客列表
     * @param param 会员id
     */
    public PageResult<PassengerQueryResult> queryList(PassengerQueryParam param) {
        logger.info("查询乘客列表的请求参数为：{}", JSON.toJSONString(param));
        Long memberId = param.getMemberId();
        PassengerExample passengerExample = new PassengerExample();
        passengerExample.setOrderByClause("id desc");
        PassengerExample.Criteria criteria = passengerExample.createCriteria();
        if (ObjUtil.isNotNull(memberId)) {
            criteria.andMemberIdEqualTo(memberId);
        }
        PageHelper.startPage(param.getPageNo(), param.getPageSize());
        List<Passenger> passengerList = passengerMapper.selectByExample(passengerExample);

        PageInfo<Passenger> passengerPageInfo = new PageInfo<>(passengerList);
        logger.info("总行数：{}", passengerPageInfo.getTotal());
        logger.info("总页数：{}", passengerPageInfo.getPages());
        List<PassengerQueryResult> passengerQueryList = BeanUtil.copyToList(passengerList, PassengerQueryResult.class);
        PageResult<PassengerQueryResult> pageResult = new PageResult<>();
        pageResult.setTotal(passengerPageInfo.getTotal());
        pageResult.setList(passengerQueryList);
        return pageResult;
    }

    public void delete(Long id) {
        passengerMapper.deleteByPrimaryKey(id);
    }
}
